import { ref } from 'vue'
import type { ConfirmOptions, ConfirmResult } from '@/utils/confirmDialog'

export function useConfirmDialog() {
  const loading = ref(false)

  /**
   * 显示确认对话框
   * @param options 对话框配置选项
   * @returns Promise<ConfirmResult> 返回用户的选择结果
   */
  const show = async (options: ConfirmOptions = {}): Promise<ConfirmResult> => {
    try {
      loading.value = true
      const message = options.message || '您确定要执行此操作吗？'
      const confirmed = window.confirm(message)
      return { confirmed, cancelled: !confirmed }
    } finally {
      loading.value = false
    }
  }

  /**
   * 显示信息确认对话框
   * @param message 确认消息
   * @param title 对话框标题
   * @returns Promise<boolean> 返回是否确认
   */
  const info = async (message: string, title?: string): Promise<boolean> => {
    try {
      loading.value = true
      const fullMessage = title ? `${title}\n\n${message}` : message
      return window.confirm(fullMessage)
    } finally {
      loading.value = false
    }
  }

  /**
   * 显示危险操作确认对话框
   * @param message 确认消息
   * @param title 对话框标题
   * @returns Promise<boolean> 返回是否确认
   */
  const danger = async (message: string, title?: string): Promise<boolean> => {
    try {
      loading.value = true
      const fullMessage = title ? `${title}\n\n${message}` : message
      return window.confirm(fullMessage)
    } finally {
      loading.value = false
    }
  }

  /**
   * 显示删除确认对话框
   * @param itemName 要删除的项目名称
   * @returns Promise<boolean> 返回是否确认删除
   */
  const deleteConfirm = async (itemName: string = '此项目'): Promise<boolean> => {
    try {
      loading.value = true
      return window.confirm(`您确定要删除 ${itemName} 吗？删除后将无法恢复。`)
    } finally {
      loading.value = false
    }
  }

  /**
   * 显示保存确认对话框
   * @param message 确认消息
   * @returns Promise<boolean> 返回是否确认保存
   */
  const saveConfirm = async (message: string = '您确定要保存当前修改吗？'): Promise<boolean> => {
    try {
      loading.value = true
      return window.confirm(message)
    } finally {
      loading.value = false
    }
  }

  /**
   * 显示退出确认对话框
   * @param message 确认消息
   * @returns Promise<boolean> 返回是否确认退出
   */
  const exitConfirm = async (message: string = '您确定要退出吗？未保存的修改将丢失。'): Promise<boolean> => {
    try {
      loading.value = true
      return window.confirm(message)
    } finally {
      loading.value = false
    }
  }

  return {
    loading,
    show,
    info,
    danger,
    deleteConfirm,
    saveConfirm,
    exitConfirm
  }
}
